package com.surpassun.emall.service.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.surpassun.emall.repository.AbstractRepository;
import com.surpassun.emall.service.AbstractService;

@Service
public class AbstractServiceImpl<T> implements AbstractService<T> {

	@Inject
	private AbstractRepository<T> abstractRepository;
	
	private Class<T> entityClass;
	
	public AbstractServiceImpl() {
	}
	
	public AbstractServiceImpl(Class<T> entityClass) {
		this.entityClass = entityClass;
	}

	@Override
	public void add(T entity) {
		getAbstractRepository().add(entity);
	}

	@Override
	public void update(T entity) {
		getAbstractRepository().update(entity);
	}

	@Override
	public void delete(T entity) {
		getAbstractRepository().delete(entity);
	}

	@Override
	public T get(long id) {
		return getAbstractRepository().get(id);
	}
	
	@Override
	public List<T> getAll() {
		return getAbstractRepository().getAll();
	}
	
	@Override
	public List<T> getRange(int start, int end) {
		return getAbstractRepository().getRange(start, end);
	}
	
	@Override
	public int count() {
		return getAbstractRepository().count();
	}

	public AbstractRepository<T> getAbstractRepository() {
		this.abstractRepository.setEntityClass(this.entityClass);
		return this.abstractRepository;
	}

	@Override
	public T getSingleByField(String fieldName, Object fieldValue, String restrictionType) {
		return getAbstractRepository().getSingleByField(fieldName, fieldValue, restrictionType);
	}

	@Override
	public List<T> getByField(String fieldName, Object fieldValue, String restrictionType) {
		return getAbstractRepository().getByField(fieldName, fieldValue, restrictionType);
	}

	@Override
	public List<T> getByFields(String[] fieldNames, Object[] fieldValues, String[] restrictionTypes) {
		return getAbstractRepository().getByFields(fieldNames, fieldValues, restrictionTypes);
	}

}
